﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronStoreDAL;

namespace BackEnd
{
	public partial class Category : Technical.UserAutentification
	{
		private ElectronStoreSource dataSource;
		private ElectronStoreDAL.Category category;
		int idCategory;


		protected void Page_Load(object sender, EventArgs e)
		{
			dataSource = new ElectronStoreSource();

			if (int.TryParse(Request.Params["id"], out idCategory))
			{
				if (idCategory > 0)
					category = dataSource.GetCategoryById(idCategory);
			}
		}


		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (category != null)
					tbxName.Text = category.Name;
			}
		}


		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (category == null)
			{
				category = new ElectronStoreDAL.Category();
				category.Name = tbxName.Text;
				if(dataSource.AddCategory(category) > 0)
					Response.Redirect(string.Format(GeneralResource.urlCatalog, category.CategoryID));
				else
					Response.Redirect(string.Format(GeneralResource.urlCatalog, 1));
			}
			else
			{
				if (category.Name != tbxName.Text)
				{
					category.Name = tbxName.Text;
					dataSource.UpdateCategory(category.CategoryID);
				}
				Response.Redirect(string.Format(GeneralResource.urlCatalog, category.CategoryID));
			}
			
		}


		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect(string.Format(GeneralResource.urlCatalog, idCategory));
		}

	}	//public partial class Category

}	//namespace BackEnd